﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ANPeCtrl : MonoBehaviour
{
    [SerializeField]
    private int startIndex;
    [SerializeField]
    private int endIndex;
    [SerializeField]
    private Transform from;
    [SerializeField]
    private Transform to;
    [SerializeField]
    private int num;


    public BBInputController inputCtrl;

    public List<ItemCtrl> icList = new List<ItemCtrl>();
    // Use this for initialization
    void Start()
    {
        Init();
        inputCtrl = BBIPhoneTouchManager.instance.inputController();
    }

    private void Init()
    {
        for (int i = 0; i < num; i++)
        {
            GameObject go = Instantiate(Resources.Load<GameObject>(Random.Range(startIndex, endIndex + 1).ToString()));
            Vector3 pos = new Vector3(Random.Range(from.position.x, to.transform.position.x), Random.Range(from.position.y, to.transform.position.y));
            go.transform.position = pos;
            go.transform.rotation = Quaternion.Euler(0, 0, Random.Range(0f, 360f));
            icList.Add(go.GetComponent<ItemCtrl>());
            go.GetComponent<SpriteRenderer>().sortingOrder = i * 2;
        }
        FlowerCtrl.totalNum = num;
        FlowerCtrl.curActiveNum = num;
    }

    void Update()
    {
        if (!inputCtrl.isConnected())
        {
            Debug.Log("连接失败");
            return;
        }
        List<Vector3> touchList = new List<Vector3>();
        for (int i = 0; i < iPhoneInput.touchCount; i++)
        {
            RaycastHit2D hit;
            Ray ray = Camera.main.ScreenPointToRay(iPhoneInput.GetTouch(i).position);
            hit = Physics2D.Raycast(ray.origin, ray.direction);
            if (hit.transform != null)
            {
                touchList.Add(hit.point);
            }
        }
        ItemCtrl.touches = touchList;
        foreach (var t in icList)
        {
            t.Check(0.3f);
        }
    }
}
